package com.stone.action;

import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.stone.model.Provider;
import com.stone.model.ProviderStone;
import com.stone.model.Stone;
import com.stone.service.IProviderService;
import com.stone.service.IProviderStoneService;
import com.stone.service.IStoneService;
import com.stone.utils.Page;

public class ProviderStoneAction  extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8052209170374022385L;
	
	/**
	 * 一些Service
	 */
	private IProviderService providerService;
	private IStoneService stoneService;
	private IProviderStoneService providerStoneService;
	
	/*Max Number Of Recently Visit Stone*/
	private final int MAXRECENTLY = 3;
	
	private boolean hasQueue = false;
	
	private Long providerId;
	private List<ProviderStone> providerStoneList ;
	private Provider provider;
	
	private int pagesize = 15;
	private int pageno = 1;
	private Page page;
	
	public String findAllPrividerStones(){
		providerStoneList = providerStoneService.findAllPrividerStoneByProviderId(providerId); 
		return SUCCESS;
	}
	
	public String findAllPrividerStonesByPage(){
		setQueue();
		checkPopular();
		provider = providerService.findProviderById(providerId);
		page = providerStoneService.findAllPrividerStoneByProviderIdByPage(providerId,pageno,pagesize); 
		return SUCCESS;
	}
	
	/**
	 * Get Visit Stone from Session Scop
	 */
	private void setQueue(){
		Map session = ActionContext.getContext().getSession();
		Queue<Stone> queue = (Queue<Stone>)session.get("recent");
		if(queue==null){
			queue = new ArrayBlockingQueue<Stone>(MAXRECENTLY);
			session.put("recent", queue);
		}
		hasQueue = (queue.size()>0)?true:false;
	}
	/**
	 *  check whether the popular stones are in the session 
	 */
	private void checkPopular(){
		Map session = ActionContext.getContext().getSession();
		if(session.get("popular")==null){
			//List<Stone> popularStones = stoneService.getVisitMostStones(1, 6).getList();
			List<Stone> popularStones = stoneService.getVisitMostDisplayStones(1, 6).getList();
			session.put("popular", popularStones);
		}
	}
	
	//----------set get
	public IProviderService getProviderService() {
		return providerService;
	}
	public void setProviderService(IProviderService providerService) {
		this.providerService = providerService;
	}
	public IStoneService getStoneService() {
		return stoneService;
	}
	public void setStoneService(IStoneService stoneService) {
		this.stoneService = stoneService;
	}

	public IProviderStoneService getProviderStoneService() {
		return providerStoneService;
	}

	public void setProviderStoneService(IProviderStoneService providerStoneService) {
		this.providerStoneService = providerStoneService;
	}

	public Long getProviderId() {
		return providerId;
	}

	public void setProviderId(Long providerId) {
		this.providerId = providerId;
	}

	public List<ProviderStone> getProviderStoneList() {
		return providerStoneList;
	}

	public void setProviderStoneList(List<ProviderStone> providerStoneList) {
		this.providerStoneList = providerStoneList;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getPageno() {
		return pageno;
	}

	public void setPageno(int pageno) {
		this.pageno = pageno;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public boolean isHasQueue() {
		return hasQueue;
	}

	public void setHasQueue(boolean hasQueue) {
		this.hasQueue = hasQueue;
	}

	public Provider getProvider() {
		return provider;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}
	
}
